﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using System.IO;
using Telerik.Web.UI;
using Telerik.Web.UI.Upload;
using System.Threading;

public partial class Admin_UserControls_AlbumManagement : System.Web.UI.UserControl
{
    private static String StrAlbumName;
    private static String StrDescription;
    private static int AlbumID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //Panel1.Visible = true;
            //Panel2.Visible = false;
            //Panel3.Visible = false;
            //Panel4.Visible = false;
        }
    }
    protected void RadGrid1_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Xem")
        {
            String AlbumDetail = "AlbumDetail.aspx?AlbumID=";
            GridDataItem item = (GridDataItem)e.Item;
            AlbumDetail += item["album_id"].Text.Trim();
            Response.Redirect(AlbumDetail);
            return;
        }
        if (e.CommandName == "Them")
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            return;
        }

        if (e.CommandName == "Sua")
        {
            String AlbumDetail = "AlbumDetail.aspx?AlbumID=";
            GridDataItem item = (GridDataItem)e.Item;
            AlbumDetail += item["album_id"].Text.Trim();
            Response.Redirect(AlbumDetail);
            
            return;
        }

        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            AlbumID = int.Parse(item["album_id"].Text);
            Album.Delete(AlbumID);
            return;
        }

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        DataTable dt = Album.GetAll();
        dt.Columns.Add("username");
        foreach (DataRow dtRow in dt.Rows)
        {
            User user = User.GetInfo((int)dtRow["user_id"]);
            dtRow["username"] = user.UserName;
        }
        RadGrid1.DataSource = dt;
        RadGrid1.DataBind();
    }
    protected void Button_OK_Click(object sender, EventArgs e)
    {
        StrAlbumName = TextBox_AlbumName.Text.Trim();
        StrDescription = TextBox_Description.Text.Trim();
        Panel2.Visible = false;
        Panel3.Visible = true;
    }
    protected void Button_Upload_Click(object sender, EventArgs e)
    {
        if (RadUpload1.UploadedFiles.Count > 0)
        {
            // Tao Album
            Album newAlbum = new Album();
            newAlbum.AlbumName = "Test";// StrAlbumName;
            newAlbum.AlbumDescription = "Test";// StrDescription;
            DateTime now = DateTime.Now;
            newAlbum.CreateAlbumDate = now;
            newAlbum.LastModifyDate = now;
            newAlbum.IsShown = true;
            newAlbum.UserID = 1;
            String StrAlbumPath = Server.MapPath("~/Files/Album");
            if (!Directory.Exists(StrAlbumPath))
            {
                return;
            }
            try
            {
                newAlbum.Path = "duc";

//                     String.Format("_{0, 2:00}_{1, 2:00}_{2, 2:00}_{3,3:000}_{4,2:00}_{5,2:00}_{6,4:0000}",
//                     now.Hour, now.Minute, now.Second, now.Millisecond, now.Day, now.Month, now.Year);
                int AlbumID = Album.Insert(newAlbum);
                if (AlbumID != -1)
                {
                    StrAlbumPath = StrAlbumPath + "\\" + newAlbum.Path+ "_"+ AlbumID;
                    Directory.CreateDirectory(StrAlbumPath);
                    newAlbum = Album.GetInfo(AlbumID);
                    newAlbum.Path = "duc_" + AlbumID.ToString();
                    Album.Update(newAlbum);
                    int newPhotoID = -1;
                    foreach (UploadedFile FileUpload in RadUpload1.UploadedFiles)
                    {
                        if (!Response.IsClientConnected)
                        {
                            break;
                        }
                        Photo newPhoto = new Photo(AlbumID);
                        newPhotoID = Photo.Insert(newPhoto);
                        if (newPhotoID != -1)
                        {
                            FileUpload.SaveAs(StrAlbumPath + "\\" + newPhotoID + FileUpload.GetExtension());
                            newPhoto = Photo.GetInfo(newPhotoID);
                            newPhoto.FileName = newPhotoID + FileUpload.GetExtension();
                            newPhoto.PhotoCreateDate = DateTime.Now;
                            newPhoto.PhotoCaption = "";
                            newPhoto.IsShown = true;
                            Photo.Update(newPhoto);
                        }
                    }
                }

                String AlbumDetail = "AlbumDetail.aspx?AlbumID=" + AlbumID.ToString();
                Response.Redirect(AlbumDetail);

            }
            catch (System.Exception Ex)
            {
                Label_ResultUpload.Text = Ex.ToString();
            }

        }
    }
}
